<template>
	<view>
		<view v-if="isCanUse">
			<view>
				<view class="header">
					<image src="../../../static/wx.png"></image>
				</view>
				<view class="content">
					<view>申请获取以下权限</view>
					<text>获得你的公开信息(昵称，头像、地区等)</text>
					<text>授权后点击登录</text>
				</view>

				<button class="bottom" open-type="getUserInfo" type="primary" @click="shou" v-if="!isShow">授权</button>
				<button class="bottom" type="primary" v-if="isShow" @click="login">点击登录</button>
				<!-- <button class="bottom" type="primary" open-type="getUserInfo" withCredentials="true" lang="zh_CN" @getuserinfo="wxGetUserInfo">授权登录</button> -->
			</view>
		</view>
	</view>
</template>

<script>
	import api from '../../../static/js/api.js'
	import com from '../../../static/js/com.js'
	import win from '../../../static/js/win.js'
	export default {
		data() {
			return {
				SessionKey: '',
				OpenId: '',
				nickName: null,
				avatarUrl: null,
				isCanUse: uni.getStorageSync('isCanUse') || true, //默认为true
				userInfo: [],
				parent: '',
				isShow: false
			};
		},
		onLoad(options) {
			let that = this;
			console.log(options)
			if (options.id) {
				this.parent = options.id
			}
			if (options.q !== undefined) {
				let q = decodeURIComponent(options.q);
				console.log(q)
				let bedcodeParams = that.getQueryVariable(q, 'p');
				console.log(bedcodeParams)
				that.parent = bedcodeParams;
				uni.setStorageSync('parent',bedcodeParams)
			}
		},
		onShow() {
			let parent = uni.getStorageSync('parent')
			this.parent = parent
		},
		methods: {
			shou() {
				this.isShow = true
			},
			getQueryVariable(query, variable) {
				let vars = query.split('&');
				for (let i = 0; i < vars.length; i++) {
					let pair = vars[i].split('=');
					if (pair.length == 2) {
						return pair[1];
					}
					if (pair[0] == variable) {
						return pair[1];
					}
				}
				return false;
			},
			wxGetUserInfo() {
				let _this = this;
				uni.getUserInfo({
					provider: 'weixin',
					success: function(infoRes) {
						let nickName = infoRes.userInfo.nickName; //昵称
						let avatarUrl = infoRes.userInfo.avatarUrl; //头像
						try {
							uni.setStorageSync('isCanUse', false); //记录是否第一次授权  false:表示不是第一次授权
							_this.updateUserInfo();
						} catch (e) {}
					},
					fail(res) {}
				});
			}, //登录

			login() {
				let _this = this;
				uni.showLoading({
					title: '登录中...',

				});

				// 1.wx获取登录用户code
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						console.log(loginRes)
						let code = loginRes.code;
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								console.log(infoRes)
								//获取用户信息后向调用信息更新方法
								let nickName = infoRes.userInfo.nickName; //昵称
								let avatarUrl = infoRes.userInfo.avatarUrl; //头像
								let parent = uni.getStorageSync('parent')
								api.get('/api/user/reg/wx_mini_app',{code,name:nickName,pic:avatarUrl,parent},function(res){
									uni.hideLoading()
									console.log(res)
									if(res.s == 1){
										com.setLocData('auth_token',res.d.apiToken)
										com.setLocData('login',1)
										com.showToast('登陆成功')
										win.gohome();
									}
								})
							}
						});
					}
				});
			},
			getOpenId(code) {}
		}
	};
</script>

<style>
	.header {
		margin: 90rpx 0 90rpx 50rpx;
		border-bottom: 1px solid #ccc;
		text-align: center;
		width: 650rpx;
		height: 300rpx;
		line-height: 450rpx;
	}

	.header image {
		width: 200rpx;
		height: 200rpx;
	}

	.content {
		margin-left: 50rpx;
		margin-bottom: 90rpx;
	}

	.content text {
		display: block;
		color: #9d9d9d;
		margin-top: 40rpx;
	}

	.bottom {
		border-radius: 80rpx;
		margin: 70rpx 50rpx;
		font-size: 35rpx;
	}
</style>
